#!/usr/bin/python3
from helper_function import * 
class NoDeps(Exception):
    pass

class NoMoreDeps(Exception):
    pass
def required_check(script):
    try:
        for letters in script:
            if len(letters) != 1:
                raise NotString

        if not "/" in script:
            full_name = match_name_with_cat(script)

        else:
            full_name = script
        stripped_name = full_name.split("/")[1]
        info_file = "{0}/{1}/{2}.info".format(tree, full_name, stripped_name)
        info_file = open(info_file).readlines()
        for lines in info_file:
            if 'REQUIRES=""' in lines:
                raise NoDeps

            elif 'REQUIRES="' in lines:
                required_line = lines.rstrip()
                required_line = required_line.split('"')[1]
                return required_line.split()
    
            
        
    except AttributeError:
        print("bad name")
        return 2

    except NoDeps:
        return 2
    except NotString:
        return 2


def dep_two_depth(script):
    if required_check(script) != 2:
        try:
            type_num = type(2)
    
            deps = [required_check(x) for x in required_check(script)]
            for items in deps:
                if type(items) == type(2):
                    raise NoMoreDeps

                else:
                    return True
        except NoMoreDeps:
            return False


def loop_required(script):
    try:
        if required_check(script) != 2:
            if dep_two_depth(script):
                final = []
                finallest = []
                fdep = required_check(script)
                while fdep:
                    fdep = sub_list(fdep)
                    final.extend(fdep)
                    fdep = [required_check(x) for x in fdep]

                
                final.reverse()
                for items in final:
                    if items not in finallest:
                        finallest.append(items)
                finallest.reverse()
                return finallest

            else:
                return(required_check(script))
        else:
            return("{0} do not requires anything".format(script))

    except:
        raise



